package com.wc.alorithm_luogu._国赛模拟赛.C_进制;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/5/19 9:35
 * @description https://www.luogu.com.cn/problem/T448165?contestId=170180
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 200;
    static int[] num = new int[N];
    static long[] base = new long[N];
    static int len = 0;
    static int[][] map = new int[][]{
            {0, 0, 0},
            {1, 2, 0},
            {2, 0, 1},
            {0, 2, 1}
    };

    public static void main(String[] args) {
        long n = sc.nextLong();
        long x = n;
        int q = sc.nextInt();
        base[0] = 1;
        for (int i = 1; i <= 50; i++) {
            base[i] = base[i - 1] * 3;
        }
        while (x > 0) {
            num[len++] = (int)(x % 3);
            x /= 3;
        }
        int op, i, pre;
        while (q-- > 0) {
            op = sc.nextInt();
            i = sc.nextInt();
            pre = num[i];
            num[i] = map[op][num[i]];
            n +=  base[i] * (num[i] - pre);
            out.println(n);
        }
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
